<template>
  <div class="edit-item-wrap">
    <div class="item">{{label}}</div>
    <div class="item editable"
      v-html="text"
      :contenteditable="!isChange"
      @input="changeText"
      @blur="blurFunc($event)">
      </div>
    <div class="item" v-if="showIcon"><i class="iconfont icon-edit" @click="isChange=false"></i></div>
  </div>
</template>

<script>
export default {
  name: 'edit-item',
  props: {
    value: {
      type: String,
      default: ''
    },
    label: {
      type: String,
      default: ''
    },
    edit: {
      type: Boolean,
      default: true
    },
    showIcon: {
      type: Boolean,
      default: true
    },
    keepEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      text: this.value,
      isChange: this.edit
    }
  },
  watch: {
    /* value(n1, nv2) {
      if (this.isChange) {
        this.text = n1
      }
    } */
    value() {
      if (this.isChange) {
        this.text = this.value
      }
    }
  },
  methods: {
    changeText() {
      this.$emit('input', this.$el.innerText)
    },
    blurFunc(ev) {
      if (!this.keepEdit) {
        this.isChange = true
      }
      this.$emit('blurFunc')
    }
  }
}
</script>

<style lang="scss" scoped>
.edit-item-wrap {
  display: flex;
  flex-direction: row;
  align-items: center;
  font-size: 12px;
  width: 100%;
  height: 100%;
  .item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    height: 80%;
    margin-right: 10px;
  }
  .iconfont {
    font-size: 16px;
    cursor: pointer;
  }
  .editable {
    min-width: 50px;
    max-width: 100px;
    /* overflow-x: auto; */
    overflow: hidden;
    &[contenteditable="true"] {
      background: #ffffff;
      color: #333333;
    }
    &[contenteditable="true"]:active,
    &[contenteditable="true"]:focus{
      border:none;
      outline:none;
    }
  }
}
</style>
